﻿using System;
using System.Collections;
using System.Collections.Generic;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using Extensions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Gameplay.YGUICustom;
using Yoozoo.Mars.Got;

public class RpgBubbleSizeFitter : MonoBehaviour
{
    public YEmojiText text;
    public RectTransform rootRect;
    public RectTransform bubbleRect;
    public ContentSizeFitter fitter;
    public HorizontalLayoutGroup layout;
    private bool isAutoSize = true;
    private RectTransform parentRect;
    public float portraitMaxWidth = 300;// 竖版最大宽度

    private void Update()
    {
        Resize();
    }

    public void Resize()
    {
        bool IsPortrait = ScreenOrientManager.GetInstance().IsPortrait();
        if (parentRect == null)
        {
            parentRect = transform.parent.GetComponent<RectTransform>();
        }
        var width = text.preferredWidth + layout.padding.left + layout.padding.right;
        var margin = rootRect.anchoredPosition.x + bubbleRect.anchoredPosition.x + width;
        var diff = parentRect.GetWidth() / 2f - margin - 5;
        if (diff < 0)
        {
            bubbleRect.SetWidth(width + diff);
        }
        else if (diff == 0)
        {
            
        }
        else
        {
            float _width = (IsPortrait && portraitMaxWidth < width) ? portraitMaxWidth : width;
            bubbleRect.SetWidth(_width);
        }    
    }
}
